package cn.kwq.project_manger.dtoUtils;

import cn.kwq.project_manger.pojo.Conv;
import cn.kwq.project_manger.pojo.dto.ConvDto;
import cn.kwq.project_manger.pojo.dto.ConvLRDto;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;

/**
 * Created with IntelliJ IDEA.
 * 对话转换算法 现已用chat socket版本
 * @Author: kwq
 * @Date: 2023/02/23/14:09
 * @Description:
 */
@Data
@Component
@Slf4j
@Deprecated
public class SwitchConvDtoUtils {

    /**
     * 区分对话左右
     * @param fromId 对话发送者
     * @param fetch 对话集合
     * @return 转换后对象
     */
    public List<ConvLRDto> Conv2LR(Long fromId, List<Conv> fetch) {
        if (fetch.size()<40){
            return   fetch.stream()
                    .map(
                            conv -> ConvLRDto.builder()
                                    .conv(conv)
                                    .LorR(!fromId.equals(conv.getFromId()) ?"LEFT":"RIGHT")
                                    .build())
                    .collect(Collectors.toList());
        }else {
            return   fetch.stream()
                    .parallel()//多线程处理
                    .map(
                            conv -> ConvLRDto.builder()
                                    .conv(conv)
                                    .LorR(!fromId.equals(conv.getFromId()) ?"LEFT":"RIGHT")
                                    .build())
                    .collect(Collectors.toList());
        }

    }

    /**
     * 对话数据分发到各对话的算法
     * @param fromId 对话发送者
     * @param fetch 对话集合
     * @return 转换后对象
     */
    public ConvDto ConvDto2List(Long fromId, List<Conv> fetch){
        ConcurrentHashMap<Long,List<Conv>> map=new ConcurrentHashMap<>();
        fetch.stream()
                .parallel()//多线程处理
                .forEach(conv -> {
                    //当该key不存在时
                    if (map.isEmpty()||!map.containsKey(conv.getConvId())){
                        List<Conv> list =new CopyOnWriteArrayList<>();
                        list.add(conv);
                        map.put(conv.getConvId(),list);
                    }
                    //key存在时就添加到他对应的list里
                    if (map.containsKey(conv.getConvId())){
                        map.get(conv.getConvId()).add(conv);
                    }
                });
        ConcurrentHashMap<Long, List<ConvLRDto>> convDto =new ConcurrentHashMap<>();
        map.entrySet()
                .stream()
                .parallel()
                .forEach(mapSet->{

                    Long Key=mapSet.getKey();
                    List<Conv> convs=mapSet.getValue();

                    //按时间排序
                    convs.sort(comparing(Conv::getConvMsgId).reversed());
                    //反转
                    List<ConvLRDto> collect = Conv2LR(fromId, convs).stream().distinct().collect(Collectors.toList());

                    convDto.put(Key, collect);

                });
        return ConvDto
                .builder()
                .convDto(convDto)
                .build();
    }

    private Long unReadCount(List<Conv> conv) {
        return   conv.stream().filter(i -> i.getIsRead() == 0).count();
    }
}
